package touro.mancala.master;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

import touro.mancala.core.Constants;

/**
 * Listen on port 5770 for clients to connect
 * and then spawn off threads listening for client boards
 * @author Student
 *
 */
public class ClientAcceptListener implements Runnable {
	
	private static final Logger logger = Logger.getLogger(ClientAcceptListener.class.getName());

	private ServerSocket serverSocket;
	private boolean closed;
	private Server server;

	public ClientAcceptListener(Server server) throws IOException{
		serverSocket = new ServerSocket(Constants.CLIENT_PORT);
		closed = false;
		this.server = server;
		logger.log(Level.INFO, "Listening for Clients on " + Constants.CLIENT_PORT);
	}
	
	public void run(){
		try {			
			while (!closed) {
				logger.log(Level.INFO, "Accepting Client Connections");
				Socket socket = serverSocket.accept();
				handleSocket(socket, server);				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void handleSocket(Socket s, Server server){
		logger.log(Level.INFO, "Handling Client connection from " + s.getInetAddress());
		ClientSocketHandler handler = new ClientSocketHandler (s, server);
		Thread thread = new Thread(handler);
		thread.start();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
	}
	
}
